// console.log( '构建开发环境的包' )
// 要求：快速构建，快速响应
let { task, src, dest, watch, series, parallel } = require('gulp')
let load = require('gulp-load-plugins')()
let del = require('del')

// 删除dist目录
task('del', async() => {
    del('./dist')
})

// 拷贝图片
task('imgs', async() => {
    src('./imgs/*.*')
        .pipe(dest('./dist/imgs'))
        .pipe(load.connect.reload())
})

// 拷贝html
task('html', async() => {
    src('./view/*.html')
        .pipe(dest('./dist'))
        .pipe(load.connect.reload())
})

// 拷贝font
task('font', async() => {
    src('./font/*.*')
        .pipe(dest('./dist/font'))
        .pipe(load.connect.reload())
})

// 拷贝lib
task('lib', async() => {
        src('./lib/*.*')
            .pipe(dest('./dist/lib'))
            .pipe(load.connect.reload())
    })
    // 拷贝data
task('data', async() => {
        src('./data/*.*')
            .pipe(dest('./dist/data'))
            .pipe(load.connect.reload())
    })
    //拷贝php
task('php', async() => {
    src('./php/*.*')
        .pipe(dest('./dist/php'))
        .pipe(load.connect.reload())
})

// 编译sass
task('sass', async() => {
    src('./sass/*.scss')
        .pipe(load.sass())
        .pipe(dest('./dist/css'))
        .pipe(load.connect.reload())
})

// 转成ES5
task('toES5', async() => {
    src('./script/*.js')
        .pipe(load.babel({ presets: ['@babel/env'] }))
        .pipe(dest('./dist/js'))
        .pipe(load.connect.reload())
})

// 启动web服务
task('webserver', async() => {
    load.connect.server({
        root: './dist',
        livereload: true,
        port: 3001
    })
})

// 监听文件变化
task('watchs', async() => {
    watch('./imgs/*.*', series('imgs', 'html', 'sass'))
    watch('./view/*.html', series('html'))
    watch('./sass/*.scss', series('sass'))
    watch('./script/*.js', series('toES5'))
    watch('./script/lib/*.*', series('lib'))
    watch('./data/*.*', series('data'))
    watch('./php/*.*', series('php'))
})

// 打包
task('build', series('del', 'imgs', 'html', 'font', 'lib', 'sass', 'toES5', 'data', 'php'))

// 组合任务
task('dev', series('build', 'webserver', 'watchs', 'data', 'php'))